<?php
/**
 * 调用微信的接口
 */
namespace Home\Controller;
use Think\Controller;
class ApiController extends CommonController {
	/**
	 * [index 测试获取AccessToken]
	 * @Author   Xuebingsi
	 * @DateTime 2017-02-08T11:59:11+0800
	 * @return   [type]                   [description]
	 */
    public function index(){

    	$access_token = $this->getAccessToken();
    	
       
    }
    /**
     * [getAccessToken 获取AccessToken]
     * @Author   Xuebingsi
     * @DateTime 2017-02-08T11:59:54+0800
     * @return   [type]                   [description]
     */
    public function getAccessToken()
    {
    	if(isset($_SESSION['access_token']) && $_SESSION['expires_time'] > time()){

    		return $_SESSION['access_token'];

    	}else{

    		$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APPSECRET;

	    	$data = $this->wget($url);

	    	$_SESSION['access_token'] = $data['access_token'];
	    	$_SESSION['expires_time'] = time()+7200;

    		return $_SESSION['access_token'];
    	}
    	
    }
    /**
     * [setmenu 设置菜单]
     * @Author   Xuebingsi
     * @DateTime 2017-02-08T12:00:03+0800
     * @return   [type]                   [description]
     */
    public function setmenu()
    {
    	$access_token = $this->getAccessToken();
    	$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;

    	$data = array(
    		'button'=>array(
    			array(
    				'type'=>'click',
          			"name"=>"今日歌曲",
    				"key"=>"V1001_TODAY_MUSIC"
    				),
    			array(
    				'type'=>'view',
          			"name"=>"百度",
    				"url"=>"http://www.baidu.com/"
    				),
    			array(
    				 "name"=>"二级菜单",
    				 'sub_button'=>array(
    				 	array(
    				 		"type"=>"view",
				             "name"=>"SOSO",
				             "url"=>"http://www.soso.com/"
    				 		),
    				 	array(
    				 		"type"=>"view",
				             "name"=>"BBS",
				             "url"=>"http://bbs.xuebingsi.com/"
    				 		),
    				 	array(
    				 		"type"=>"view",
				             "name"=>"BAIDU",
				             "url"=>"http://www.baidu.com/"
    				 		),
    				 	),

    				)
    			)
    		);

    	$res=  $this->wpost($url,$data);

    	var_dump($res);
    	
    }
    /**
     * [send 群发消息]
     * @Author   Xuebingsi
     * @DateTime 2017-02-08T12:00:19+0800
     * @return   [type]                   [description]
     */
    public function send()
    {
    	$access_token = $this->getAccessToken();
    	$url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=".$access_token;

    	$data = array(
    		'touser'=>array(
    			'oF5Z2v5yzYfsCYP2oEDfz4ns94p0',
    			'oF5Z2vy3FqN5frmWmTAygpdQ3a5Y',
    			'oF5Z2v2jfPYNducaiwapQZD3mFJs',
    			'oF5Z2vwJc9aeqniw3Ald-ydpyW_M'
    			),
    		"voice"=>array(
    			'media_id'=>"O_wF2uVGATQDKcFOMOtbxC53NMQ9sV4MfJCiD8ESzgLMi6lNasjIT59ixcx05Qek"
    			),
    		'msgtype'=>"voice"
    		);

    	$data = array(
    		'touser'=>array(
    			'oF5Z2v5yzYfsCYP2oEDfz4ns94p0',
    			'oF5Z2vy3FqN5frmWmTAygpdQ3a5Y',
    			'oF5Z2v2jfPYNducaiwapQZD3mFJs',
    			'oF5Z2vwJc9aeqniw3Ald-ydpyW_M'
    			),
    		"text"=>array(
    			'content'=>"大家好这个是群发的"
    			),
    		'msgtype'=>"text"
    		);

    	$res=  $this->wpost($url,$data);

    	var_dump($res);

    }
    /**
     * [sendMoban 发送消息]
     * @Author   Xuebingsi
     * @DateTime 2017-02-08T12:00:33+0800
     * @return   [type]                   [description]
     */
    public function sendMoban()
    {
    	$access_token = $this->getAccessToken();
    	$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;

    	$data = array(
    		'touser'=>"oF5Z2v5yzYfsCYP2oEDfz4ns94p0",
    		'template_id'=>"CQQkgL16s7n7PezCDvR5ktaLjQ9KWG_digc_KhYA8xI",
    		'url'=>"http://bbs.xuebingsi.com",
    		'data'=>array(
    			'addr'=>array(
    				'value'=>'奥园广场',
    				"color"=>"#173177"
    				),
    			'time'=>array(
    				'value'=>'2017-12-10 12:09:00',
    				"color"=>"#173177"
    				),
    			'money'=>array(
    				'value'=>'120005656.00',
    				"color"=>"#173177"
    				),
    			),
    		);

    	
    	$res=  $this->wpost($url,$data);

    	var_dump($res);

    }
}